package com.zhuiyun.pm.client.address.controller;

import com.zhuiyun.pm.client.address.entity.ClientAddress;
import com.zhuiyun.pm.client.address.service.servicelmpl.AddressServiceImpl;
import com.zhuiyun.pm.config.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AddressController
 * Description T0D0
 * Author Administrator
 * @Date 2023/3/14
 * Version 1.0
 **/
@RestController
@RequestMapping("address")
@CrossOrigin
public class AddressController {
    @Autowired
    AddressServiceImpl addressServiceImpl;

    /**
     * 查询买家地址
     * @author 温
     * @date 2023/3/14 19:59
     * @parameter [clientAddress]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @GetMapping("/addressList")
    public CommonResult addressList(ClientAddress clientAddress) {
        List<ClientAddress> clientAddresses = addressServiceImpl.clientAddressList(clientAddress);
        return CommonResult.ok(clientAddresses);
    }

    /**
     * 添加买家新地址
     * @author 温
     * @date 2023/3/14 19:59
     * @parameter [clientAddress]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/addressAdd")
    public CommonResult addressAdd(@RequestBody ClientAddress clientAddress) {
        int i = addressServiceImpl.clientAddressAdd(clientAddress);
        if (i > 0) {
            return CommonResult.ok();
        } else {
            return CommonResult.error("添加失败");
        }
    }

    /**
     * 修改买家地址
     * @author 温
     * @date 2023/3/14 20:00
     * @parameter [clientAddress]
     * @return com.zhuiyun.pm.config.CommonResult
     */
    @PostMapping("/addressUpdate")
    public CommonResult addressUpdate(@RequestBody ClientAddress clientAddress){
        int i = addressServiceImpl.clientAddressUpdate(clientAddress);
        if(i>0){
            return CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }
/**
 * 逻辑删除买家地址
 * @author 温
 * @date 2023/3/14 20:39
 * @parameter [id]
 * @return com.zhuiyun.pm.config.CommonResult
 */
    @GetMapping("/addressDelInfo")
    public CommonResult addressDelInfo(int id){
        int i = addressServiceImpl.delAddressInfo(id);
        return CommonResult.ok(i);
    }
//
//    /**
//     * 根据id查询地址详细信息
//     * @author 温
//     * @date 2023/3/15 9:30
//     * @parameter [id]
//     * @return com.zhuiyun.pm.config.CommonResult
//     */
//  @GetMapping("/addressId")
//    public CommonResult  addressId(int id){
//      ClientAddress clientAddress = addressServiceImpl.clientId(id);
//      return CommonResult.ok(clientAddress);
//  }

    @GetMapping("/getDefaultAddByUserId")
    public CommonResult getDefaultAddByUserId(Integer userId){
        ClientAddress clientAddress  = addressServiceImpl.getDefaultAddByUserId(userId);
        return CommonResult.ok(clientAddress);
    }

    @GetMapping("/updateUserDefaultAddr")
    public CommonResult updateUserDefaultAddr(Integer userId,Integer AddrId){
        addressServiceImpl.updateUserDefaultAddr(userId,AddrId);
        return CommonResult.ok();
    }
}